// components/selection/selection.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    propId: {
      type: Array,
      value: []
    },
    propName: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value_sel: [0, 0, 0], //对应数组下标
    province_s: [],
    city_s: [],
    area_s: [],
    areaInfo_name: [],
    area_id_Info: [],
    price_name: '',
    city_name: '',
    area_name: '',
    province: 0,
    city: 0,
    area: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击所在地区弹出选择框
    selectDistrict: function (e) {
      var that = this
      if (that.data.addressMenuIsShow) {
        return
      }
      that.startAddressAnimation(true)
    },

    // 执行动画
    startAddressAnimation: function (isShow) {
      var that = this
      if (isShow) {
        that.animation.translateY(0 + 'vh').step()
      } else {
        that.animation.translateY(40 + 'vh').step()
      }
      that.setData({
        animationAddressMenu: that.animation.export(),
        addressMenuIsShow: isShow,
      })
    },

    // 点击地区选择取消按钮
    cityCancel: function (e) {
      this.startAddressAnimation(false)
    },

    // 点击地区选择确定按钮
    citySure: function (e) {
      var that = this
      var value_sel = that.data.value_sel;
      //console.log(value_sel)
      that.startAddressAnimation(false)
      // 将选择的城市信息放入数组后期方便操作
      var areaInfo_name = []; //区域名称
      areaInfo_name.push(that.data.province_s[value_sel[0]].name);
      areaInfo_name.push(that.data.city_s[value_sel[1]].name);
      areaInfo_name.push(that.data.area_s[value_sel[2]].name);
      var area_id_Info = []; //区域id
      area_id_Info.push(that.data.province_s[value_sel[0]].id);
      area_id_Info.push(that.data.city_s[value_sel[1]].id);
      area_id_Info.push(that.data.area_s[value_sel[2]].id);
      that.setData({
        areaInfo_name: areaInfo_name,
        area_id_Info: area_id_Info,
        price_name: areaInfo_name[0],
        city_name: areaInfo_name[1],
        area_name: areaInfo_name[2],
        province: area_id_Info[0],
        city: area_id_Info[1],
        area: area_id_Info[2],
      })

      var address = {} // detail对象，提供给事件监听函数
      address.areaInfo_name = that.data.areaInfo_name;
      address.area_id_Info = that.data.area_id_Info;
      //console.log(address)
      that.triggerEvent('mySelection', {
        address
      })



    },

    //点击黑色区域关闭地址选择
    hideCitySelected: function (e) {
      this.startAddressAnimation(false)
    },

    // 处理省市县联动逻辑
    cityChange: function (e) {
      var that = this;
      var value_sel = e.detail.value; //获取选中的id数组
      var province = this.data.province;
      var citys = this.data.citys;
      var areas = this.data.areas;
      var province_Num = value_sel[0];
      var city_Num = value_sel[1];
      var county_Num = value_sel[2];
      //判断是否需要切换市区级
      if (this.data.value_sel[0] != province_Num) {
        var provinceid = that.data.province_s[province_Num].id; //获取省id查询市级信息
        that.getCity(provinceid); //获取市级信息
        this.setData({
          value_sel: [province_Num, 0, 0],
        })
      } else if (this.data.value_sel[1] != city_Num) { //判断是否需要切换区域
        var cityid = that.data.city_s[city_Num].id;
        that.getArea(cityid); //获取区域信息
        this.setData({
          value_sel: [province_Num, city_Num, 0],
        })
      } else {
        this.setData({
          value_sel: [province_Num, city_Num, county_Num]
        })
      }
    },

    //获取省级信息 getProvince
    getProvince: function () {
      var that = this;
      app.ajax({
        url: 'Common/Common/getProvince',
        method: "POST",
        data: {},
        success: function (res) {
          if (res.data.code == 1000) {
            let province_s = res.data.data;
            that.setData({
              province_s: province_s
            })
            //初次进入才会调用 获取市区级
            if (that.data.city_s.length == 0) {
              var idArr = that.data.propId;
              var nameArr = that.data.propName;

              let [province = ''] = [idArr[0]];
              let [price_name = ''] = [nameArr[0]];

              let value_sel = that.data.value_sel;
              let i = 0;
              let isHave = false;
              for (; i < province_s.length; i++) {
                if (province_s[i].id == province) {
                  value_sel[0] = i;
                  isHave = true;
                  that.getCity(province_s[i].id);
                  that.setData({
                    value_sel: value_sel,
                    price_name: price_name,
                    province: province
                  })
                }
              }
              if (!isHave) {
                that.getCity(province_s[0].id);
              }
              //console.log(that.data.value_sel)
            }
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    },
    //获取市信息 getCity
    getCity: function (provinceid) {
      var that = this;
      app.ajax({
        url: 'Common/Common/getCity',
        method: "POST",
        data: {
          id: provinceid
        },
        success: function (res) {
          if (res.data.code == 1000) {
            let city_s = res.data.data;
            that.setData({
              city_s: city_s
            });
            //重置区域信息 

            var idArr = that.data.propId;
            var nameArr = that.data.propName;

            let [city = ''] = [idArr[1]];
            let [city_name = ''] = [nameArr[1]];

            let value_sel = that.data.value_sel;
            let i = 0;
            let isHave = false;
            for (; i < city_s.length; i++) {
              if (city_s[i].id == city) {
                value_sel[1] = i;
                isHave = true;
                that.getArea(city_s[i].id);
                that.setData({
                  value_sel: value_sel,
                  city_name: city_name,
                  city: city
                })
              }
            }
            if (!isHave) {
              that.getArea(city_s[0].id);
            }

            //console.log(that.data.value_sel)

            //that.getArea(res.data.data[0].id);
          } else {
            app.showToast("网络错误,请稍后重试", "none", 2000, function () {});
          }
        }
      })
    },
    //获取区域信息 getArea
    getArea: function (cityid) {
      var that = this;
      app.ajax({
        url: 'Common/Common/getArea',
        method: "POST",
        data: {
          id: cityid
        },
        success: function (res) {
          if (res.data.code == 1000) {
            let area_s = res.data.data;
            that.setData({
              area_s: area_s
            })
            var idArr = that.data.propId;
            var nameArr = that.data.propName;

            let [area = ''] = [idArr[2]];
            let [area_name = ''] = [nameArr[2]];

            let value_sel = that.data.value_sel;
            let i = 0;

            for (; i < area_s.length; i++) {
              if (area_s[i].id == area) {
                value_sel[2] = i;
                that.setData({
                  value_sel: value_sel,
                  area_name: area_name,
                  area: area
                })
              }
            }

          } else {
            app.showToast("网络错误,请稍后重试", "none", 2000, function () {});
          }
        }
      })
    },
    // //判断省市区index
    // getIndex(){
    //   var idArr = this.data.propId;
    //   var nameArr = this.data.propName;
    //   var that = this;

    //   let [province = '', city = '', area = ''] = idArr;
    //   let [price_name = '', city_name = '', area_name = ''] = nameArr;

    //   let value_sel = that.data.value_sel;
    //   let i=0;
    //   let province_s = that.data.province_s;
    //   let city_s = that.data.city_s;
    //   let area_s = that.data.area_s;
    //   for (; i < province_s.length; i++){
    //     if(province_s[i].id == province){
    //       value_sel[0] = i;
    //     }
    //   }
    //   for (; i < city_s.length; i++) {
    //     console.log(city_s[i])
    //     if (city_s[i].id == city) {
    //       value_sel[1] = i;
    //     }
    //   }
    //   for (; i < area_s.length; i++) {
    //     if (area_s[i].id == area) {
    //       value_sel[2] = i;
    //     }
    //   }
    //   console.log(value_sel)

    //   // value_sel: [0, 0, 0], //对应数组下标
    //   //   province_s: [],
    //   //     city_s: [],
    //   //       area_s: [],
    //   //         areaInfo_name: [],
    //   //           area_id_Info: [],
    //   //             price_name: '',
    //   //               city_name: '',
    //   //                 area_name: '',
    //   //                   province: 0,
    //   //                     city: 0,
    //   //                       area: 0,

    // },
    //根据地址设置更换地址省的默认值
    setProDefault: function (info) {
      console.log(info)
      var that = this;
      var province_s = that.data.province_s;
      console.log(province_s)
      for (var i in province_s) {

        if (province_s[i].id == info[0]) {
          var a = 'value_sel[0]';
          that.setData({
            [a]: parseInt(i)
          })

          app.ajax({
            url: 'Common/Areas/areas',
            method: "POST",
            data: {
              parent_id: province_s[i].id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                // console.log(res.data.data);          
                that.setData({
                  city_s: res.data.data
                });
                that.setCityDefault(info);
              } else {
                app.showToast(res.data.message);
              }
            }
          })
        }
      }

    },
    //根据地址设置更换地址市的默认值
    setCityDefault: function (info) {
      var that = this;
      var city_s = that.data.city_s;
      for (var i in city_s) {
        if (city_s[i].id == info[1]) {
          var a = 'value_sel[1]';
          that.setData({
            [a]: parseInt(i)
          })
          app.ajax({
            url: 'Common/Areas/areas',
            method: "POST",
            data: {
              parent_id: city_s[i].id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                // console.log(res.data.data);          
                that.setData({
                  area_s: res.data.data
                });
                that.setAreaDefault(info);
              } else {
                app.showToast(res.data.message);
              }
            }
          })
          break;
        }
      }
    },
    //根据地址设置更换地址区的默认值
    setAreaDefault: function (info) {
      var that = this;
      var area_s = that.data.area_s;
      for (var i in area_s) {
        if (area_s[i].id == info[2]) {
          var a = 'value_sel[2]';
          that.setData({
            [a]: parseInt(i)
          })
          break;
        }
      }
    },
  },
  ready() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;
    that.getProvince(); //获取省份信息
  },
  attached() {

    // this.setProDefault(this.data.propId);

  },
  externalClasses: ['selection']
})